home *** CD-ROM | disk | FTP | other *** search
- (****************************************************************
- *
- * Name: SHAREA
- *
- * Function: share memory/data with another process
- *
- * Shows how to: 1. allocate and deallocate shared memory.
- * 2. read from and write to shared memory.
- * 3. mail to another process the address of shared data.
- * 4. control access to shared data via mailbox semaphore.
- *
- ****************************************************************)
-
- program ShareA;
-
- uses DVAPI;
-
- const
-
- (* minimum API version required *)
- REQUIRED = $201;
- PIFLEN = 416;
- ONEBLOCK = 1;
-
- var
-
- (* API version number *)
- version : integer;
-
- (* TFDD text file *)
- tfd : text;
-
- (* PIF-related variables *)
- fp : file;
- dvpbuf : array [0..415] of byte;
-
- (* application handle of other process *)
- apphanb : ULONG;
-
- type
-
- (* type declarations related to shared data *)
- DATATYPE = string;
- DATAPTR = ^DATATYPE;
-
- const
-
- (* constant value to be assigned to shared memory *)
- SHRCONST : DATATYPE = 'AAAAA ';
-
- var
-
- (* pointer to shared data *)
- shrptr : DATAPTR;
-
- (* mailbox semaphore controlling access to shared memory *)
- sema : ULONG;
-
- const
-
- (* global name of mailbox *)
- name : string = 'Shared Memory Semaphore';
-
-
- (********************************************************************
- (* program_body
- (*
- (* Set up named mailbox semaphore. Start other process. Allocate
- (* & initialize shared memory. Mail pointer to shared data.
- (* Continuously read, display and modify contents of shared data.
- (********************************************************************)
-
- procedure program_body;
- begin
-
- (* open TFDD *)
- tfd_open (tfd,win_me);
-
- (* create & name mailbox semaphore *)
- sema := mal_new;
- mal_sname (sema,name);
-
- (* disallow closing of window *)
- win_disallow (win_me,ALW_CLOSE);
-
- (* read other process' dvp file into buffer area *)
- assign (fp,'c:\dv\SB-PIF.DVP');
- reset (fp,PIFLEN);
- blockread (fp,dvpbuf,ONEBLOCK);
- close (fp);
-
- (* start other process & get its task handle *)
- apphanb := app_start (@dvpbuf,PIFLEN);
-
- (* allocate shared memory & get its buffer pointer *)
- (* Api_getmem normally allocates "system memory" from within the
- (* "process memory" pool. Such system memory is not shareable among other
- (* processes. Instead, if system memory were to be allocated from the
- (* "shared memory" pool then system memory would be shareable among other
- (* processes. In other words, all processes that use shared memory have
- (* access to each others' system memory. Placing a single asterisk (*)
- (* in the Shared Memory Pathname field of the PIF file will accomplish this.
- (* See Chapter 16: Memory Management of the API Reference Manual. *)
- shrptr := api_getmem (sizeof (SHRCONST));
-
- (* copy initial data into shared memory *)
- shrptr^ := SHRCONST;
-
- (* mail to other process the pointer to shared data *)
- mal_write (mal_of (apphanb),@shrptr,sizeof (shrptr));
-
- (* begin critical region *)
- api_beginc;
-
- (* loop till handle of other process is no longer valid *)
- while (api_isobj (apphanb)) do
- begin
-
- (* lock semaphore *)
- mal_lock (sema);
-
- (* read & display current contents & address of shared data *)
- writeln (tfd,shrptr^,' at ',seg (shrptr^),':',ofs (shrptr^));
-
- (* modify contents of shared data *)
- shrptr^ := SHRCONST;
-
- (* unlock semaphore *)
- mal_unlock (sema);
-
- (* end critical region *)
- api_endc;
-
- (* begin critical region *)
- api_beginc;
-
- end;
-
- (* free allocated shared memory *)
- api_putmem (shrptr);
-
- (* allow closing of window *)
- win_allow (win_me,ALW_CLOSE);
-
- (* free allocated object *)
- mal_free (sema);
-
- (* close TFDD *)
- tfd_close (tfd);
-
- end;
-
-
- (**********************************************************************
- * main - check for DESQview present and enable required extensions.
- ***********************************************************************)
-
- begin
-
- (* initialize Pascal interfaces and get API version number *)
- version := api_init;
-
- (* if DESQview is not running or version is too low, display a message *)
- if (version < REQUIRED) then
- writeln ('This program requires DESQview version ',REQUIRED div 256,
- '.',(REQUIRED mod 256) div 16,(REQUIRED mod 256) mod 16,' or later.')
-
- else
- begin
-
- (* tell DESQview what extensions to enable and start application *)
- api_level (REQUIRED);
- program_body;
-
- (* disable Pascal interfaces and return from program *)
- api_exit;
-
- end;
-
- end.